<?php 

namespace app\common;

require_once __DIR__.'/Log.php';
use \GatewayWorker\Lib\Gateway; //？

use app\common\Log;


/**
 * 基类
 */
class Base 
{
    protected $log = null;
    protected $api = null;

	function __construct()
	{
        $this->log = new Log();
	}

    // 日志
    public function log($data='',$level='normal')
    {
        $this->log->log($data,$level);
    }

    public function save_api($value='')
    {
        $this->api = $value;
    }

	/**
     * 向客户端发送信息
     */
    public function outMessage($code = 0,$message = "request success",$data = array(),$status = 'ok',$count = 0)
    {   
        $codeMap = array('0'=>'success','111'=>'未知错误');
        if ($code!=0) {
            // $status = 'error';
            if (!$message) {
                $message = $codeMap[$code];
            }
        }
        $data = [
            'api' => $this->api,
            'code' => $code,
            'message' => $message,
            'status' => $status,
            'type' => $_SESSION['returnType'],
            'count' => $count,
            'data' => $data
        ];
        if($data){
            $this->log('向用户发送数据');
            $this->log($data);

            return Gateway::sendToCurrentClient(json_encode($data)."\r\n");
        }else{
            abort(404);
        }
    }





}




 ?>